//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baibei.module.stock.basic;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;

import com.baibei.widget.stock.ChartType;
import com.baibei.widget.stock.RaeCombinedChart;
import com.baibei.widget.stock.RaeXAxis;
import com.baibei.widget.stock.render.RaeXAxisRender;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.text.DecimalFormat;
import java.util.List;

public class MinuteChart extends RaeCombinedChart {
    private final DecimalFormat mDecimalFormat = new DecimalFormat();
    private RaeXAxis mRaeXAxis;
    protected float mLimitValue = (float) (0.0F / 0.0);

    public MinuteChart(Context context) {
        super(context);
    }

    public MinuteChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MinuteChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected void init() {
        super.init();
        IAxisValueFormatter formatter = new IAxisValueFormatter() {
            public String getFormattedValue(float value, AxisBase axis) {
                int index = (int)value;
                if(MinuteChart.this.mRaeXAxis.getXAxisValues() != null && MinuteChart.this.mRaeXAxis.getXAxisValues().size() >= 0) {
                    List<String> values = MinuteChart.this.mRaeXAxis.getXAxisValues();
                    return (String)values.get(index % values.size());
                } else {
                    return String.valueOf(value);
                }
            }
        };
        this.mXAxis.setValueFormatter(formatter);
        this.mXAxis = this.mRaeXAxis = new RaeXAxis();
        this.getXAxis().setValueFormatter(formatter);
        this.getAxisLeft().setValueFormatter(new IAxisValueFormatter() {
            public String getFormattedValue(float value, AxisBase axis) {
                MinuteChart.this.mDecimalFormat.applyPattern("###,##0.00");
                return MinuteChart.this.mDecimalFormat.format((double)value);
            }
        });
        this.getAxisRight().setValueFormatter(new IAxisValueFormatter() {
            public String getFormattedValue(float value, AxisBase axis) {
                if(MinuteChart.this.mLimitValue <= 0.0F) {
                    return "0.00%";
                } else {
                    float percent = -(1.0F - value / MinuteChart.this.mLimitValue);
                    if(Float.isNaN(percent)) {
                        return "0.00%";
                    } else {
                        MinuteChart.this.mDecimalFormat.applyPattern("0.00%");
                        return MinuteChart.this.mDecimalFormat.format((double)percent);
                    }
                }
            }
        });
    }

    protected void initChart() {
        this.mXAxisRenderer = new RaeXAxisRender(this.mViewPortHandler, this.mXAxis, this, this.mLeftAxisTransformer);
    }

    public ChartType getDefaultChartType() {
        return ChartType.TIME_LINE;
    }

    public void setData(List<String> xValues, List<Entry> yValues, long limitValue) {
        this.mLimitValue = (float)limitValue;
        this.mXAxisLabels = xValues;
        int entryCount = yValues.size();
        if(xValues.size() > yValues.size()) {
            for(int i = yValues.size(); i < xValues.size(); ++i) {
                yValues.add(new Entry((float)i, (float) (0.0F / 0.0)));
            }
        }

        LineDataSet dataSet = new LineDataSet(yValues, "");
        LineData lineData = new LineData(new ILineDataSet[]{dataSet});
        this.mRaeXAxis.setXAxisValues(xValues);
        CombinedData combinedData = new CombinedData();
        combinedData.setData(lineData);
        this.setData(combinedData);
        this.setLimitValue((float)limitValue);
        this.mTheme.loadDataSetTheme(new DataSet[]{dataSet});
        this.moveViewToX((float)entryCount);
        this.setVisibleXRangeMinimum(30.0F);
    }

    public void highlightValue(Highlight high, boolean callListener) {
        if(high != null) {
            Entry e = ((CombinedData)this.mData).getEntryForHighlight(high);
            if(e != null && Float.isNaN(e.getY())) {
                high = null;
            }
        }

        super.highlightValue(high, callListener);
    }

    public void setLimitValue(float limitValue) {
        this.getAxisLeft().removeAllLimitLines();
        LimitLine limitLine = new LimitLine(limitValue, (String)null);
        limitLine.setLineWidth(1.0F);
        limitLine.enableDashedLine(8.0F, 8.0F, 0.0F);
        limitLine.setLineColor(Color.parseColor("#29B2FF"));
        limitLine.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
        this.getAxisLeft().addLimitLine(limitLine);
    }
}
